import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";

export type UserResult = {
  success: boolean;
  data: {
    /** 用户名 */
    username: string;
    /** 当前登陆用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式毫秒时间戳） */
    expires: number;
  };
  msg: string;
};

export type fileResult = {
  success: boolean;
  msg: string;
  data: any;
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式毫秒时间戳） */
    expires: number;
  };
  msg: string;
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("login"), { data });
};
/** 退出 */
export const logout = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("logout"), { data });
};

/** 注册 */
export const register = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("register"), { data });
};

/** 修改*/
export const changpwd = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("changpwd"), { data });
};
/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", baseUrlApi("refreshToken"), {
    data
  });
};

/** 查询所有用户*/
export const getAllUsers = (data?: object) => {
  return http.request<fileResult>("post", baseUrlApi("getAllUsers"), { data });
};

/** 新增用户*/
export const addUser = (data?: object) => {
  return http.request<fileResult>("post", baseUrlApi("addUser"), { data });
};

/** 编辑用户*/
export const editUser = (data?: object) => {
  return http.request<fileResult>("post", baseUrlApi("editUser"), { data });
};

/** 重置密码*/
export const resetPassword = (data?: object) => {
  return http.request<fileResult>("post", baseUrlApi("resetPassword"), {
    data
  });
};

/** 激活用户*/
export const enableUser = (data?: object) => {
  return http.request<fileResult>("post", baseUrlApi("enableUser"), { data });
};

/** 禁用用户*/
export const disableUser = (data?: object) => {
  return http.request<fileResult>("post", baseUrlApi("disableUser"), { data });
};

/** 删除用户*/
export const deleteUser = (data?: object) => {
  return http.request<fileResult>("post", baseUrlApi("deleteUser"), { data });
};
/* 测试 */
export const test = () => {
  return http.request("get", baseUrlApi(""));
};
